package sunwin.yois.web.controller;

import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import sunwin.yois.web.common.R;
import sunwin.yois.web.common.YoisImageHelper;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.comm.model.NameAndValueModel;
import sunwin.yoms.domain.park.ParkCooperateDomain;
import sunwin.yoms.service.system.ParkCooperateService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by watson on 2015/9/15.
 */
@Controller
public class ParkCooperate {
    @Autowired
    private ParkCooperateService parkCooperateService;

    @RequestMapping(value = "park-index", name = "park/", method = RequestMethod.GET)
    public void Park(ModelMap modelMap,
            @RequestParam(required = false, defaultValue = "2") int tid,
            @RequestParam(required = false, defaultValue = "1") int pix) throws YomsException {
    /*    YomsPage yomsPage = new YomsPage();
        yomsPage.setPageIndex(pageIndex);
        List<ParkCooperateDomain> domains = parkCooperateService.getParkByTimes(yomsPage);
        modelMap.addAttribute("parks", domains);
        modelMap.addAttribute("pages", yomsPage);*/
        modelMap.addAttribute("tid", tid);
        modelMap.addAttribute("pix", pix);
        getParkByTime(modelMap,tid,pix);
    }

    /**
     * 根据合作的规模获取分页新闻
     *
     * @param modelMap
     * @param tid
     * @param pix
     */
    private void getParkByTime(ModelMap modelMap,int tid,int pix)throws YomsException{
        YomsPage yomsPage=new YomsPage();
        yomsPage.setPageSize(20);
        yomsPage.setPageIndex(pix);

        List<ParkCooperateDomain> domains=parkCooperateService.getParkByTimes(yomsPage);
        for (ParkCooperateDomain domain :domains){
            String content=domain.getParkContent();
            String parkContent=YoisImageHelper.delHtmlTag(content);
            if (parkContent.length() > 200){
                String aTag= String.format("<a href='%s?nid=%s'>详细</a>", R.getParkDetailed(), domain.getParkId());
                parkContent= parkContent.substring(0, 200) + "..." + aTag;
            }
            domain.setParkContent(parkContent);
        }
        List<NameAndValueModel> pages = new ArrayList<>(0);
        for (int i=0;i<yomsPage.getPageCount();i++){
            NameAndValueModel mavm=new NameAndValueModel();
            mavm.setValue(String.format("/%s?tid=%s&pix=%s", R.getParkIndex(), tid, i + 1));
            mavm.setName(i + 1 + "");
            pages.add(mavm);
            if (i == 9) {
                break;
            }
        }
        modelMap.addAttribute("pages",pages);
        modelMap.addAttribute("parks",domains);
        modelMap.addAttribute("pol",yomsPage.getPageCount());
    }



    /**
     * 根据id获取单条公园信息
     */
    @RequestMapping(value = R.ParkDetailed, name = "park/", method = RequestMethod.GET)
    public void getParkInfo(ModelMap modelMap,
                                @RequestParam(required = false, defaultValue = "0") int nid) throws YomsException {
        modelMap.addAttribute("nid", nid);
        ParkCooperateDomain domain = parkCooperateService.getParkById(nid);
        String content = YoisImageHelper.changeImgSrc(domain.getParkContent(), "detailed");
        domain.setParkContent(content);
        //StringUtil.isBlank判断字符串是否为空，长度为0
        if (StringUtil.isBlank(domain.getParkContent())) {
            domain.setParkContent("...");
        }
        modelMap.addAttribute("parkDomain", domain);
        //无样式新闻内容
        String parkContent = YoisImageHelper.delHtmlTag(content);
        modelMap.addAttribute("parkContent", parkContent);
    }
}
